package com.lee.blog.controller;

import com.google.gson.Gson;
import com.lee.blog.bean.Article;
import com.lee.blog.bean.Message;
import com.lee.blog.bean.Time;
import com.lee.blog.service.ArticleService;
import com.lee.blog.service.MessageService;
import com.lee.blog.service.TimeService;
import com.lee.blog.utils.MdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.bind.annotation.XmlType;
import java.util.List;

@Controller
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @Autowired
    MessageService messageService;

    @Autowired
    TimeService timeService;


    //进入首页
    @RequestMapping("/home")
    public String home(Model model, HttpSession session,
                       HttpServletRequest request,
                       @RequestParam(value = "page", defaultValue = "1") Integer page){

        //page:当前页码


        //每一页文章数量
        int itemCount = 5;

        //文章开始索引
        int start = (page - 1) * itemCount;
        //获取点击总人数
        int uv = timeService.getUv();
        //获取分页文章
        List<Article> homeArticle = articleService.getHomeArticle(start, itemCount);
        //获取最新文章
        List<Article> newArticles = articleService.getNewArticle();
        //获取最新留言
        List<Message> newMessage = messageService.getNewMessage();
        //获取文章总数
        int articleCount = articleService.getArticleCount();
        //总页数
        int totalPage = articleCount % itemCount == 0 ? articleCount / itemCount : articleCount / itemCount + 1;

        //获取留言总数
        int messageCount = messageService.getMessageCount();

        model.addAttribute("uv", uv);
        model.addAttribute("homeArticle", homeArticle);
        model.addAttribute("newArticles", newArticles);
        model.addAttribute("newMessage", newMessage);
        model.addAttribute("articleCount", articleCount);
        model.addAttribute("messageCount", messageCount);
        model.addAttribute("page",page);
        model.addAttribute("totalPage", totalPage);

        return "home";
    }

    //进入文章详情
    @RequestMapping("/articleDetail")
    public String articleDetail(@RequestParam("id") Integer id, Model model){
        System.out.println(id);
        Article article = articleService.getArticleById(id);
        String content = article.getContent();
        String convertContent = MdUtils.convert(content);
        article.setContent(convertContent);
        model.addAttribute("article",article);
        return "article";
    }

    //进入技术页面
    @RequestMapping("/technology")
    public String technology(Model model){
        List<Article> technologyArticle = articleService.getTechnologyArticle();
        List<String> allLabel = articleService.getAllLabel();
        model.addAttribute("allLabel",allLabel);
        model.addAttribute("technologyArticle",technologyArticle);
        return "technology";
    }

    //进入杂记页面
    @RequestMapping("/life")
    public String life(Model model){
        List<Article> lifeArticle = articleService.getLifeArticle();
        model.addAttribute("lifeArticle",lifeArticle);
        return "life";
    }

    //进入时间轴页面
    @RequestMapping("/time")
    public String time(Model model){
        List<Time> times = timeService.getTime();
        model.addAttribute("times",times);
        return "time";
    }

    //进入留言页面
    @RequestMapping("/message")
    public String message(Model model){
        List<Message> allMessage = messageService.getAllMessage();
        model.addAttribute("allMessage",allMessage);
        return "message";
    }

    //提交留言
    @PostMapping("/doMessage")
    public String doMessage(Message message){
        messageService.doMessage(message);
        return "redirect:/message";
    }

    //进入关于我页面
    @RequestMapping("/about")
    public String about(){
        return "about";
    }

    //根据标签获取文章json数据
    @GetMapping("/getArticleByLabel")
    @ResponseBody
    public String getArticleByLabel(@RequestParam("label") String label){
        System.out.println(label);
        List<Article> articleByLabel = articleService.getArticleByLabel(label);
        System.out.println(articleByLabel);
        Gson gson = new Gson();
        String articleJson = gson.toJson(articleByLabel);
        System.out.println(articleJson);
        return articleJson;
    }
}
